#!/bin/bash
# HAProxy 日志表和物化视图部署脚本
# 使用方法：./deploy_haproxy_logs.sh

set -e

CLICKHOUSE_CLIENT="clickhouse-client"
CLICKHOUSE_HOST="${CLICKHOUSE_HOST:-127.0.0.1}"
CLICKHOUSE_PORT="${CLICKHOUSE_PORT:-8123}"
CLICKHOUSE_DB="${CLICKHOUSE_DB:-default}"

echo "=== HAProxy 日志表和物化视图部署脚本 ==="
echo "ClickHouse 主机: ${CLICKHOUSE_HOST}:${CLICKHOUSE_PORT}"
echo "数据库: ${CLICKHOUSE_DB}"
echo ""

# 检查 ClickHouse 连接
echo "1. 检查 ClickHouse 连接..."
if ! $CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="SELECT 1" > /dev/null 2>&1; then
    echo "❌ 无法连接到 ClickHouse，请检查连接参数"
    exit 1
fi
echo "✅ ClickHouse 连接正常"

# 检查 otel_logs 表是否存在
echo ""
echo "2. 检查 otel_logs 表..."
if ! $CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="EXISTS TABLE ${CLICKHOUSE_DB}.otel_logs" | grep -q "1"; then
    echo "❌ otel_logs 表不存在，请先确保 OTEL Collector 正常运行"
    exit 1
fi
echo "✅ otel_logs 表存在"

# 执行表创建
echo ""
echo "3. 创建 haproxy_logs 目标表..."
$CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --database=$CLICKHOUSE_DB < haproxy_logs_schema.sql

echo "✅ 表和物化视图创建完成"

# 验证部署结果
echo ""
echo "4. 验证部署结果..."

# 检查表是否创建成功
HAPROXY_TABLE_EXISTS=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="EXISTS TABLE ${CLICKHOUSE_DB}.haproxy_logs")
HAPROXY_MV_EXISTS=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="EXISTS TABLE ${CLICKHOUSE_DB}.haproxy_logs_mv")

if [[ "$HAPROXY_TABLE_EXISTS" == "1" && "$HAPROXY_MV_EXISTS" == "1" ]]; then
    echo "✅ haproxy_logs 表和物化视图创建成功"
else
    echo "❌ 表或物化视图创建失败"
    exit 1
fi

# 显示数据统计
echo ""
echo "5. 数据统计："
OTEL_LOGS_COUNT=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="SELECT COUNT(*) FROM ${CLICKHOUSE_DB}.otel_logs")
HAPROXY_LOGS_COUNT=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="SELECT COUNT(*) FROM ${CLICKHOUSE_DB}.haproxy_logs")
OTEL_HAPROXY_COUNT=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="SELECT COUNT(*) FROM ${CLICKHOUSE_DB}.otel_logs WHERE JSONHas(Body, 'level')")

echo "   - otel_logs 总记录数: $OTEL_LOGS_COUNT"
echo "   - otel_logs 中 HAProxy 日志数: $OTEL_HAPROXY_COUNT"
echo "   - haproxy_logs 表记录数: $HAPROXY_LOGS_COUNT"

# 如果需要同步历史数据
if [[ "$HAPROXY_LOGS_COUNT" -lt "$OTEL_HAPROXY_COUNT" ]]; then
    echo ""
    echo "⚠️  检测到历史数据未同步"
    read -p "是否要同步历史数据到 haproxy_logs 表？(y/N): " -r
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "正在同步历史数据..."
        $CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="
        INSERT INTO ${CLICKHOUSE_DB}.haproxy_logs
        SELECT
            Timestamp,
            JSONExtractString(Body, 'timestamp') as log_timestamp,
            JSONExtractString(Body, 'client_ip') as client_ip,
            JSONExtractString(Body, 'client_port') as client_port,
            JSONExtractString(Body, 'level') as level,
            JSONExtract(Body, 'ssl_info', 'Map(String, String)')['is_resumed'] as ssl_is_resumed,
            JSONExtract(Body, 'ssl_info', 'Map(String, String)')['sni'] as ssl_sni,
            JSONExtract(Body, 'ssl_info', 'Map(String, String)')['ssl_version'] as ssl_version,
            JSONExtract(Body, 'ssl_info', 'Map(String, String)')['cipher'] as ssl_cipher,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['frontend'], '') as access_frontend,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['backend'], '') as access_backend,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['server'], '') as access_server,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['time_duration'], '') as access_time_duration,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['status_code'], '') as access_status_code,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['bytes_read'], '') as access_bytes_read,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['bytes_sent'], '') as access_bytes_sent,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['user_agent'], '') as access_user_agent,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['http_method'], '') as access_http_method,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['host'], '') as access_host,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['http_uri'], '') as access_http_uri,
            if(JSONExtractString(Body, 'level') = 'info', JSONExtract(Body, 'access', 'Map(String, String)')['http_version'], '') as access_http_version,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['termination_state'], '') as error_termination_state,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['fc_error'], '') as error_fc_error,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['ssl_fc_error'], '') as error_ssl_fc_error,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['ssl_fc_error_hex'], '') as error_ssl_fc_error_hex,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['ssl_c_error'], '') as error_ssl_c_error,
            if(JSONExtractString(Body, 'level') = 'error', JSONExtract(Body, 'errors', 'Map(String, String)')['ssl_ca_error'], '') as error_ssl_ca_error
        FROM ${CLICKHOUSE_DB}.otel_logs
        WHERE JSONHas(Body, 'level') AND JSONExtractString(Body, 'level') IN ('info', 'error')
        AND Timestamp NOT IN (SELECT Timestamp FROM ${CLICKHOUSE_DB}.haproxy_logs)
        "
        
        NEW_COUNT=$($CLICKHOUSE_CLIENT --host=$CLICKHOUSE_HOST --port=$CLICKHOUSE_PORT --query="SELECT COUNT(*) FROM ${CLICKHOUSE_DB}.haproxy_logs")
        echo "✅ 历史数据同步完成，当前记录数: $NEW_COUNT"
    fi
fi

echo ""
echo "🎉 部署完成！"
echo ""
echo "后续操作："
echo "1. 查看示例查询: cat haproxy_logs_queries.sql"
echo "2. 验证数据: clickhouse-client --query=\"SELECT COUNT(*) FROM default.haproxy_logs\""
echo "3. 查看最新日志: clickhouse-client --query=\"SELECT * FROM default.haproxy_logs ORDER BY Timestamp DESC LIMIT 5\""